package top.lyjwn.ajui.widget.image;


import android.graphics.Rect;
import android.os.Parcel;
import android.os.Parcelable;

import com.xuexiang.xui.widget.imageview.preview.enitity.IPreviewInfo;

import androidx.annotation.Nullable;

public class ImageInfo implements IPreviewInfo {

    private String  url;
    private Rect  bounds;
    private String  videoUrl;

    public ImageInfo() {
    }

    @Override
    public String getUrl() {
        return url;
    }

    @Override
    public Rect getBounds() {
        return bounds;
    }

    @Nullable
    @Override
    public String getVideoUrl() {
        return videoUrl;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setBounds(Rect bounds) {
        this.bounds = bounds;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }

    @Override
    public int describeContents() {
        return 0;
    }


    /**
     * 必须实现Parcelable.Creator接口，否则在获取该类数据的时候会报错
     *
     * android.os.BadParcelableException:Parcelable protocol requires a
     * Parcelable.Creator object called CREATOR on class
     *
     * Parcelable.Creator接口实现了从Parcel容器读取本对象（Teacher）数据，并返回该对象给逻辑层使用
     *
     * Parcelable.Creator接口对象名必须为CREATOR,否则同样会报上述同样的错误；
     *
     * 在读取Parcel容器中的数据事，必须按成员变量声明的顺序来读取数据，不然会出现获取数据错误；
     *
     * 反序列化对象
     */
    public static final Parcelable.Creator<ImageInfo> CREATOR = new Creator<ImageInfo>() {

        @Override
        public ImageInfo createFromParcel(Parcel source) {
            // 必须按成员变量的顺序读取数据，不然会出现获取数据报错
            ImageInfo imageInfo = new ImageInfo();
            imageInfo.setUrl(source.readString());
            Rect rect = source.readParcelable(Rect.class.getClassLoader());
            imageInfo.setBounds(rect);
            imageInfo.setVideoUrl(source.readString());
            return imageInfo;
        }

        @Override
        public ImageInfo[] newArray(int size) {
            return new ImageInfo[size];
        }
    };



    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // 必须按成员变量声明的顺序封装数据，不然会出现获取数据报错
        // 序列化对象
        dest.writeString(url);
        dest.writeParcelable(bounds,flags);
        dest.writeString(videoUrl);

    }

}

